<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input v-model="bindForm.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="bindForm.phone" placeholder="请输入用户手机号" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="tableData"
      style="width: 100%"
      border
      fit
    >
      <el-table-column
        prop="avatarUrl"
        label="用户头像"
      >
        <template slot-scope="{ row }">
          <el-image
            style="width: 100px; height: 100px"
            :src="row.avatarUrl"
            :preview-src-list="[row.avatarUrl]"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户昵称"
      />
      <el-table-column
        prop="phone"
        label="手机号码"
      />
      <el-table-column
        prop="createdAt"
        label="创建时间"
      >
        <!-- <template slot-scope="{ row }">
          <span>{{row.createdAt | createFilter}}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        prop="utograph"
        label="个性签名"
      />
    </el-table>
  </div>
</template>

<script>
import { getUserList } from '@/api/user.js'
// import Vue from 'vue'
import moment from 'moment'
// Vue.filter('createFilter', (data) => {
//   return moment(data).format('YYYY-MM-DD')
// })
export default {
  data() {
    return {
      bindForm: {
        username: '',
        phone: null
      },
      searchForm: {
        username: '',
        phone: null
      },
      tableData: [],
      listLoading: true,
      total: null,
      pageNo: 1,
      pageSize: 5
    }
  },
  created() {
    this.getListFun()
  },

  methods: {
    getListFun() {
      getUserList({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        username: this.searchForm.username,
        phone: this.searchForm.phone
      }).then(res => {
        console.log(res)
        this.listLoading = false// 列表请求结束，则把表格的loading关闭
        const { code, data } = res
        if (code === 200) {
          this.total = data.pagination.count
          this.tableData = data.list
          this.tableData.forEach(ele => {
            ele.createdAt = moment(ele.createdAt).format('YYYY-MM-DD')
          })
        }
      })
    },
    onSearch() {
      this.searchForm.username = this.bindForm.username
      this.searchForm.phone = this.bindForm.phone
      this.getListFun()
    },
    reset() {
      this.bindForm.username = ''
      this.bindForm.phone = null
      this.searchForm.username = ''
      this.searchForm.phone = null
      this.getListFun()
    }
  }
}
</script>

<style lang="scss" scoped>
  .demo-form-inline{
   margin-top: 20px;
 }
</style>
